package com.example.wacoffee_manage.controller;

import com.example.wacoffee_manage.Service.orderService;
import com.example.wacoffee_manage.pojo.Order;
import com.example.wacoffee_manage.pojo.TodayRevenue;
import com.example.wacoffee_manage.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private orderService orderService;

    @Transactional(rollbackFor = Exception.class)
    @PostMapping
    public Result addOrder(@RequestBody Order order) throws IOException {
        orderService.addOrder(order);
        return Result.success(order.getId());

    }

    @GetMapping("/today")
    public Result getTodayOrder(){
        TodayRevenue todayRevenue = orderService.getTodayOrder();
        return Result.success(todayRevenue);
    }

    @GetMapping("/unfinished")
    public Result getUnfinishedOrder(){
        List<Order> unfinishedOrder = orderService.getUnfinishedOrder();
        return Result.success(unfinishedOrder);
    }

    @PutMapping("/{id}")
    public Result updateOrder(@PathVariable("id") Integer id){
        orderService.updateOrder(id);
        return Result.success();
    }

    @PutMapping
    public Result updateOrder(@RequestParam int id){
        orderService.updateOrderGood(id);
        return Result.success();
    }
}
